﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel.DomainServices.Server;
using NetBay.Core.BusinessEntities.Versioning;
using Rio.Manager.Business.Repository;
using Rio.Doc.Business.Repository;
using NetBay.Core.BusinessEntities;

namespace Rio.Applications.Web.Services
{
    public partial class BusinessEntitiesDomainService : DomainService
    {

        /// <summary>
        /// Add new Versioning model to the specified FolderType identifier
        /// </summary>
        /// <param name="FolderTypeIdentifier"></param>
        /// <param name="Model"></param>
        [Invoke(HasSideEffects = true)]
        public void SetVersionModel(long FolderTypeIdentifier, VersioningModel Model)
        {
            try
            {
                NetBay.Core.Log.LogManager.Instance().LogDebug("(FolderTypeIdentifier<" + FolderTypeIdentifier.ToString() + ">)...");
                VersioningRepository.SetVersionModel(FolderTypeIdentifier, Model);
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError("Exception in SetVersionModel : " + ex.Message);
            }
        }

        /// <summary>
        /// Add new Versioning model to the specified FolderType identifier
        /// </summary>
        /// <param name="FolderTypeIdentifier"></param>
        /// <param name="Model"></param>
        [Invoke(HasSideEffects = true)]
        public void UpdateVersionModel(VersioningModel NewModel, VersioningModel OldModel, long FolderTypeId)
        {
            try
            {
                NetBay.Core.Log.LogManager.Instance().LogDebug("(ModelIdentifier<" + NewModel.Identifier.ToString() + ">)...");
                VersioningRepository.UpdateVersionModel(NewModel);

                if (NewModel.ModelType != OldModel.ModelType)
                {
                    //Set all versions to delete
                    DeleteAllDocumentVersionByFolderTypeId(FolderTypeId);
                }
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError("Exception in UpdateVersionModel : " + ex.Message);
            }
        }

        /// <summary>
        /// Get the model of the specified folder type
        /// </summary>
        /// <param name="FolderTypeIdentifier"></param>
        /// <returns></returns>
        [Invoke(HasSideEffects = true)]
        public List<VersioningModel> GetVersionModel(long FolderTypeIdentifier)
        {
            var l_oRet = new List<VersioningModel>();
            try
            {
                NetBay.Core.Log.LogManager.Instance().LogDebug("(FolderTypeIdentifier<" + FolderTypeIdentifier.ToString() + ">)...");

                VersioningModel model = VersioningRepository.GetVersionModel(FolderTypeIdentifier);
                if (model != null)
                    l_oRet.Add(model);
                else
                    return null;
            }
            catch (Exception ex)
            {
                l_oRet.Add(new VersioningModel("Exception in GetVersionModel : " + ex.Message));
                NetBay.Core.Log.LogManager.Instance().LogError("Exception in GetVersionModel : " + ex.Message);
            }
            return l_oRet;
        }

        /// <summary>
        /// Get version checkout in a foder type
        /// </summary>
        /// <param name="FolderTypeIdentifier"></param>
        /// <returns></returns>
        [Invoke(HasSideEffects = true)]
        public IEnumerable<VersioningItem> GetCheckOutVersionByFolderType(long FolderTypeIdentifier)
        {
            var l_oChechOutVersion = new List<VersioningItem>();

            try
            {
                NetBay.Core.Log.LogManager.Instance().LogDebug("(FolderTypeIdentifier<" + FolderTypeIdentifier.ToString() + ">)...");
                l_oChechOutVersion =VersioningRepository.GetCheckOutVersionByFolderType(FolderTypeIdentifier);
            }
            catch (Exception ex)
            {
                l_oChechOutVersion.Add(new VersioningItem("Exception in GetCheckOutVersionByFolderType : " + ex.Message));
                NetBay.Core.Log.LogManager.Instance().LogError("Exception in GetCheckOutVersionByFolderType : " + ex.Message);
            }
            return l_oChechOutVersion;
        }

        /// <summary>
        /// Get the model of the specified folder type
        /// </summary>
        /// <param name="FolderTypeIdentifier"></param>
        /// <returns></returns>
        [Invoke(HasSideEffects = true)]
        public ReturnBoolean HasCheckoutVersionByFolderType(long FolderTypeIdentifier)
        {
            var l_oRet = new ReturnBoolean();
            try
            {
                NetBay.Core.Log.LogManager.Instance().LogDebug("(FolderTypeIdentifier<" + FolderTypeIdentifier.ToString() + ">)...");
                l_oRet = new ReturnBoolean(VersioningRepository.HasCheckoutVersionByFolderType(FolderTypeIdentifier));
            }
            catch (Exception ex)
            {
                l_oRet = new ReturnBoolean("Exception in HasCheckoutVersion : " + ex.Message);
                NetBay.Core.Log.LogManager.Instance().LogError("Exception in HasCheckoutVersion : " + ex.Message);
            }
            return l_oRet;
        }


        /// <summary>
        /// Undo check out on the document, this drop version 
        /// </summary>
        /// <param name="p_lVersionId"></param>
        [Invoke(HasSideEffects = true)]
        public ReturnBoolean UndoCheckout(long p_lVersionId)
        {
            ReturnBoolean l_oReturnBoolean;
            try
            {
                NetBay.Core.Log.LogManager.Instance().LogDebug("(p_lVersionId<" + p_lVersionId + ">");
                l_oReturnBoolean = new ReturnBoolean(DocumentRepository.UndoCheckout(p_lVersionId));
            }
            catch (Exception ex)
            {
                l_oReturnBoolean = new ReturnBoolean("(<p_lVersionId" + p_lVersionId + ">)", ex);
                NetBay.Core.Log.LogManager.Instance().LogError("Exception in UndoCheckout : " + l_oReturnBoolean.FullErrorMessage);
            }
            return l_oReturnBoolean;
        }

        /// <summary>
        /// Checkin the version
        /// </summary>
        /// <param name="p_lVersionId"></param>
        [Invoke(HasSideEffects = true)]
        public ReturnBoolean CheckinVersion(long p_lVersionId)
        {
            ReturnBoolean l_oReturnBoolean;
            try
            {
                NetBay.Core.Log.LogManager.Instance().LogDebug("(p_lVersionId<" + p_lVersionId + ">");
                l_oReturnBoolean = new ReturnBoolean(DocumentRepository.CheckInVersion(p_lVersionId));
            }
            catch (Exception ex)
            {
                l_oReturnBoolean = new ReturnBoolean("(<p_lVersionId" + p_lVersionId + ">)", ex);
                NetBay.Core.Log.LogManager.Instance().LogError("Exception in CheckinVersion : " + l_oReturnBoolean.FullErrorMessage);
            }
            return l_oReturnBoolean;
        }

        /// <summary>
        /// Delete all document version. 
        /// </summary>
        /// <param name="p_lFolderTypeId">Folder identifier id</param>
        [Invoke(HasSideEffects = true)]
        public void DeleteAllDocumentVersionByFolderTypeId(long p_lFolderTypeId)
        {           
            try
            {
                NetBay.Core.Log.LogManager.Instance().LogDebug("VersioningDs.DeleteAllDocumentVersionByFolderTypeId: (p_lFolderTypeId<" + p_lFolderTypeId + ">");
                VersioningRepository.DeleteAllDocumentVersionByFolderTypeId(p_lFolderTypeId);
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError("Exception in VersioningDs.DeleteAllDocumentVersionByFolderTypeId: " + ex.Message);
            }           
        }

    }
}